{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-18T07:41:50.173859Z",
     "start_time": "2025-08-18T07:41:49.894230Z"
    }
   },
   "source": [
    "# 数据的导入\n",
    "import pandas as pd\n",
    "df = pd.read_csv('data/employees.csv')\n",
    "print(type(df))\n",
    "print(df.tail())\n",
    "print(df.salary.mean())\n",
    "# 数据的导出\n",
    "df = df.tail()\n",
    "df.to_csv('data/new.csv')"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "     employee_id first_name last_name     email  phone_number      job_id  \\\n",
      "102          202        Pat       Fay      PFAY  603.123.6666      MK_REP   \n",
      "103          203      Susan    Mavris   SMAVRIS  515.123.7777      HR_REP   \n",
      "104          204    Hermann      Baer     HBAER  515.123.8888      PR_REP   \n",
      "105          205    Shelley   Higgins  SHIGGINS  515.123.8080      AC_MGR   \n",
      "106          206    William     Gietz    WGIETZ  515.123.8181  AC_ACCOUNT   \n",
      "\n",
      "      salary  commission_pct  manager_id  department_id  \n",
      "102   6000.0             NaN       201.0           20.0  \n",
      "103   6500.0             NaN       101.0           40.0  \n",
      "104  10000.0             NaN       101.0           70.0  \n",
      "105  12000.0             NaN       101.0          110.0  \n",
      "106   8300.0             NaN       205.0          110.0  \n",
      "6461.682242990654\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:43:56.136063Z",
     "start_time": "2025-08-18T07:43:56.120150Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# json\n",
    "df = pd.read_json('data/person.json')\n",
    "print(type(df))\n",
    "\n",
    "import json\n",
    "with open('data/person_users.json') as f:\n",
    "    data = json.load(f)\n",
    "# print(data['users'])\n",
    "print(type(data))\n",
    "df = pd.DataFrame(data['users'])\n",
    "print(type(df))\n",
    "df"
   ],
   "id": "fb3c9bca43955bf3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "<class 'dict'>\n",
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "   id name  age                 email  is_active   join_date\n",
       "0   1  寮犱笁   28  zhangsan@example.com       True  2022-03-15\n",
       "1   2  鏉庡洓   35      lisi@example.com      False  2021-11-02\n",
       "2   3  鐜嬩簲   24    wangwu@example.com       True  2023-01-20"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>email</th>\n",
       "      <th>is_active</th>\n",
       "      <th>join_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>寮犱笁</td>\n",
       "      <td>28</td>\n",
       "      <td>zhangsan@example.com</td>\n",
       "      <td>True</td>\n",
       "      <td>2022-03-15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>鏉庡洓</td>\n",
       "      <td>35</td>\n",
       "      <td>lisi@example.com</td>\n",
       "      <td>False</td>\n",
       "      <td>2021-11-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>鐜嬩簲</td>\n",
       "      <td>24</td>\n",
       "      <td>wangwu@example.com</td>\n",
       "      <td>True</td>\n",
       "      <td>2023-01-20</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:44:55.601547Z",
     "start_time": "2025-08-18T07:44:55.571112Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 缺失值的处理\n",
    "# nan:not a number\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "s = pd.Series([12,25,np.nan, None, pd.NA])\n",
    "df = pd.DataFrame([[1,pd.NA,2],[2,3,5],[None,4,6]],columns=['第1列','第2列','第3列'])\n",
    "print(s)\n",
    "# 查看是否是缺失值\n",
    "print(s.isna())\n",
    "print(s.isnull())\n",
    "print(df.isna())\n",
    "print(df.isnull())\n",
    "print(df.isna().sum(axis=1))\n",
    "print(s.isna().sum()) #查看缺失值的个数\n",
    "\n",
    "# 剔除缺失值\n",
    "print(s.dropna())\n",
    "print('-'*30)\n",
    "print(df)\n",
    "print(df.dropna()) #剔除一整条的记录\n",
    "print(df.dropna(how='all')) #如果所有的值都是缺失值，删除这一行\n",
    "print(df.dropna(thresh=1)) #如果至少有n个值不是缺失值，就保留\n",
    "print(df.dropna(axis=1)) #剔除一整列的记录\n",
    "print(df.dropna(subset=['第1列'])) #如果某列有缺失值，则删除这一行\n",
    "\n",
    "# 填充缺失值\n",
    "df = pd.read_csv('data/weather_withna.csv')\n",
    "df.tail()\n",
    "df.isna().sum(axis=0)\n",
    "df.head()\n",
    "print(df.fillna({'temp_max':20,'wind':2.5}).tail()) #使用字典来填充\n",
    "print(df.fillna(df[['temp_max','wind']].mean()).tail()) #使用统计值来填充\n",
    "print(df.ffill().tail())#用前面的相邻值填充\n",
    "print(df.bfill().tail())#用后面的相邻值填充"
   ],
   "id": "68a1f2938c979319",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0      12\n",
      "1      25\n",
      "2     NaN\n",
      "3    None\n",
      "4    <NA>\n",
      "dtype: object\n",
      "0    False\n",
      "1    False\n",
      "2     True\n",
      "3     True\n",
      "4     True\n",
      "dtype: bool\n",
      "0    False\n",
      "1    False\n",
      "2     True\n",
      "3     True\n",
      "4     True\n",
      "dtype: bool\n",
      "     第1列    第2列    第3列\n",
      "0  False   True  False\n",
      "1  False  False  False\n",
      "2   True  False  False\n",
      "     第1列    第2列    第3列\n",
      "0  False   True  False\n",
      "1  False  False  False\n",
      "2   True  False  False\n",
      "0    1\n",
      "1    0\n",
      "2    1\n",
      "dtype: int64\n",
      "3\n",
      "0    12\n",
      "1    25\n",
      "dtype: object\n",
      "------------------------------\n",
      "   第1列   第2列  第3列\n",
      "0  1.0  <NA>    2\n",
      "1  2.0     3    5\n",
      "2  NaN     4    6\n",
      "   第1列 第2列  第3列\n",
      "1  2.0   3    5\n",
      "   第1列   第2列  第3列\n",
      "0  1.0  <NA>    2\n",
      "1  2.0     3    5\n",
      "2  NaN     4    6\n",
      "   第1列   第2列  第3列\n",
      "0  1.0  <NA>    2\n",
      "1  2.0     3    5\n",
      "2  NaN     4    6\n",
      "   第3列\n",
      "0    2\n",
      "1    5\n",
      "2    6\n",
      "   第1列   第2列  第3列\n",
      "0  1.0  <NA>    2\n",
      "1  2.0     3    5\n",
      "            date  precipitation  temp_max  temp_min  wind weather\n",
      "1456  2015-12-27            NaN      20.0       NaN   2.5     NaN\n",
      "1457  2015-12-28            NaN      20.0       NaN   2.5     NaN\n",
      "1458  2015-12-29            NaN      20.0       NaN   2.5     NaN\n",
      "1459  2015-12-30            NaN      20.0       NaN   2.5     NaN\n",
      "1460  2015-12-31           20.6      12.2       5.0   3.8    rain\n",
      "            date  precipitation   temp_max  temp_min      wind weather\n",
      "1456  2015-12-27            NaN  15.851468       NaN  3.242055     NaN\n",
      "1457  2015-12-28            NaN  15.851468       NaN  3.242055     NaN\n",
      "1458  2015-12-29            NaN  15.851468       NaN  3.242055     NaN\n",
      "1459  2015-12-30            NaN  15.851468       NaN  3.242055     NaN\n",
      "1460  2015-12-31           20.6  12.200000       5.0  3.800000    rain\n",
      "            date  precipitation  temp_max  temp_min  wind weather\n",
      "1456  2015-12-27            0.0      11.1       4.4   4.8     sun\n",
      "1457  2015-12-28            0.0      11.1       4.4   4.8     sun\n",
      "1458  2015-12-29            0.0      11.1       4.4   4.8     sun\n",
      "1459  2015-12-30            0.0      11.1       4.4   4.8     sun\n",
      "1460  2015-12-31           20.6      12.2       5.0   3.8    rain\n",
      "            date  precipitation  temp_max  temp_min  wind weather\n",
      "1456  2015-12-27           20.6      12.2       5.0   3.8    rain\n",
      "1457  2015-12-28           20.6      12.2       5.0   3.8    rain\n",
      "1458  2015-12-29           20.6      12.2       5.0   3.8    rain\n",
      "1459  2015-12-30           20.6      12.2       5.0   3.8    rain\n",
      "1460  2015-12-31           20.6      12.2       5.0   3.8    rain\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:45:28.043684Z",
     "start_time": "2025-08-18T07:45:28.036679Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 时间数据的处理\n",
    "import pandas as pd\n",
    "d = pd.Timestamp('2015-02-28 10:22')\n",
    "d1 = pd.Timestamp('2015-02-28 13:22')\n",
    "print(d)\n",
    "print(type(d))\n",
    "print(\"年：\",d.year)\n",
    "print(\"月：\",d.month)\n",
    "print(\"日：\",d.day)\n",
    "print(d.hour, d.minute, d.second)\n",
    "print(\"季度：\",d.quarter)\n",
    "print(\"是否是月底：\",d.is_month_end)\n",
    "# 方法\n",
    "print(\"星期几：\",d.day_name())\n",
    "print(\"转换为天：\",d.to_period(\"D\"))\n",
    "print(\"转换为季度：\",d1.to_period(\"Q\"))\n",
    "print(\"转换为年度：\",d1.to_period(\"Y\"))\n",
    "print(\"转换为月度：\",d1.to_period(\"M\"))\n",
    "print(\"转换为周维度：\",d1.to_period(\"W\"))"
   ],
   "id": "42698d9d58253359",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2015-02-28 10:22:00\n",
      "<class 'pandas._libs.tslibs.timestamps.Timestamp'>\n",
      "年： 2015\n",
      "月： 2\n",
      "日： 28\n",
      "10 22 0\n",
      "季度： 1\n",
      "是否是月底： True\n",
      "星期几： Saturday\n",
      "转换为天： 2015-02-28\n",
      "转换为季度： 2015Q1\n",
      "转换为年度： 2015\n",
      "转换为月度： 2015-02\n",
      "转换为周维度： 2015-02-23/2015-03-01\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:45:44.707934Z",
     "start_time": "2025-08-18T07:45:44.686757Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 字符串转换为日期类型\n",
    "a = pd.to_datetime('20150228')\n",
    "print(a)\n",
    "print(type(a))\n",
    "print(a.day_name())\n",
    "\n",
    "# dataFrame 日期转换\n",
    "df = pd.DataFrame({\n",
    "    'sales':[100,200,300],\n",
    "    'date':['20250601','20250602','20250603']\n",
    "})\n",
    "df['datetime'] = pd.to_datetime(df['date'])\n",
    "df\n",
    "print(df.info())\n",
    "print(type(df['datetime']))\n",
    "df['week']=df['datetime'].dt.day_name()\n",
    "df['datetime'].dt.year\n"
   ],
   "id": "84dd02febccd157f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2015-02-28 00:00:00\n",
      "<class 'pandas._libs.tslibs.timestamps.Timestamp'>\n",
      "Saturday\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 3 entries, 0 to 2\n",
      "Data columns (total 3 columns):\n",
      " #   Column    Non-Null Count  Dtype         \n",
      "---  ------    --------------  -----         \n",
      " 0   sales     3 non-null      int64         \n",
      " 1   date      3 non-null      object        \n",
      " 2   datetime  3 non-null      datetime64[ns]\n",
      "dtypes: datetime64[ns](1), int64(1), object(1)\n",
      "memory usage: 204.0+ bytes\n",
      "None\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0    2025\n",
       "1    2025\n",
       "2    2025\n",
       "Name: datetime, dtype: int32"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:46:02.830313Z",
     "start_time": "2025-08-18T07:46:02.815023Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# csv 日期转换\n",
    "df = pd.read_csv('data/weather.csv',parse_dates=['date'])\n",
    "df.info()\n",
    "df['date'].dt.day_name()"
   ],
   "id": "9615ca04ace1cbd8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1461 entries, 0 to 1460\n",
      "Data columns (total 6 columns):\n",
      " #   Column         Non-Null Count  Dtype         \n",
      "---  ------         --------------  -----         \n",
      " 0   date           1461 non-null   datetime64[ns]\n",
      " 1   precipitation  1461 non-null   float64       \n",
      " 2   temp_max       1461 non-null   float64       \n",
      " 3   temp_min       1461 non-null   float64       \n",
      " 4   wind           1461 non-null   float64       \n",
      " 5   weather        1461 non-null   object        \n",
      "dtypes: datetime64[ns](1), float64(4), object(1)\n",
      "memory usage: 68.6+ KB\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0          Sunday\n",
       "1          Monday\n",
       "2         Tuesday\n",
       "3       Wednesday\n",
       "4        Thursday\n",
       "          ...    \n",
       "1456       Sunday\n",
       "1457       Monday\n",
       "1458      Tuesday\n",
       "1459    Wednesday\n",
       "1460     Thursday\n",
       "Name: date, Length: 1461, dtype: object"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:46:17.347891Z",
     "start_time": "2025-08-18T07:46:17.344298Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 日期数据作为索引\n",
    "# df.set_index('date' , inplace=True)#设置原来的df的索引\n",
    "print(df.loc[\"2013-01\":\"2013-02\"])"
   ],
   "id": "e8fa0318ef92c140",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Empty DataFrame\n",
      "Columns: [date, precipitation, temp_max, temp_min, wind, weather]\n",
      "Index: []\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:46:31.812460Z",
     "start_time": "2025-08-18T07:46:31.808912Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 时间间隔\n",
    "d1 = pd.Timestamp('2013-01-15')\n",
    "d2 = pd.Timestamp('2023-02-23')\n",
    "d3 = d2-d1\n",
    "print(type(d3))\n",
    "print(d3)"
   ],
   "id": "537ef9f7fbbaa6ee",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas._libs.tslibs.timedeltas.Timedelta'>\n",
      "3691 days 00:00:00\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:46:49.201249Z",
     "start_time": "2025-08-18T07:46:49.189560Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_csv('data/weather.csv',parse_dates=['date'])\n",
    "df.info()\n",
    "df['delta'] = df['date'] - df['date'][0]\n",
    "df.set_index('delta',inplace=True)"
   ],
   "id": "c5db70f487d5af35",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1461 entries, 0 to 1460\n",
      "Data columns (total 6 columns):\n",
      " #   Column         Non-Null Count  Dtype         \n",
      "---  ------         --------------  -----         \n",
      " 0   date           1461 non-null   datetime64[ns]\n",
      " 1   precipitation  1461 non-null   float64       \n",
      " 2   temp_max       1461 non-null   float64       \n",
      " 3   temp_min       1461 non-null   float64       \n",
      " 4   wind           1461 non-null   float64       \n",
      " 5   weather        1461 non-null   object        \n",
      "dtypes: datetime64[ns](1), float64(4), object(1)\n",
      "memory usage: 68.6+ KB\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:47:02.285594Z",
     "start_time": "2025-08-18T07:47:02.277067Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df\n",
    "print(df.loc['10 days':'20 days'])"
   ],
   "id": "ec7106ead7cce8d7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              date  precipitation  temp_max  temp_min  wind weather\n",
      "delta                                                              \n",
      "10 days 2012-01-11            0.0       6.1      -1.1   5.1     sun\n",
      "11 days 2012-01-12            0.0       6.1      -1.7   1.9     sun\n",
      "12 days 2012-01-13            0.0       5.0      -2.8   1.3     sun\n",
      "13 days 2012-01-14            4.1       4.4       0.6   5.3    snow\n",
      "14 days 2012-01-15            5.3       1.1      -3.3   3.2    snow\n",
      "15 days 2012-01-16            2.5       1.7      -2.8   5.0    snow\n",
      "16 days 2012-01-17            8.1       3.3       0.0   5.6    snow\n",
      "17 days 2012-01-18           19.8       0.0      -2.8   5.0    snow\n",
      "18 days 2012-01-19           15.2      -1.1      -2.8   1.6    snow\n",
      "19 days 2012-01-20           13.5       7.2      -1.1   2.3    snow\n",
      "20 days 2012-01-21            3.0       8.3       3.3   8.2    rain\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:47:29.578124Z",
     "start_time": "2025-08-18T07:47:29.573292Z"
    }
   },
   "cell_type": "code",
   "source": [
    "days = pd.date_range(\"2025-07-03\",\"2026-02-09\",freq=\"W\")\n",
    "days = pd.date_range(\"2025-07-03\",periods=10,freq=\"QE\")\n",
    "print(days)"
   ],
   "id": "995891215e85739b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DatetimeIndex(['2025-09-30', '2025-12-31', '2026-03-31', '2026-06-30',\n",
      "               '2026-09-30', '2026-12-31', '2027-03-31', '2027-06-30',\n",
      "               '2027-09-30', '2027-12-31'],\n",
      "              dtype='datetime64[ns]', freq='QE-DEC')\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:48:02.342326Z",
     "start_time": "2025-08-18T07:48:02.338577Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "data = {\n",
    "    \"name\":['alice','alice','bob','alice','jack','bob'],\n",
    "    \"age\":[26,25,30,25,35,30],\n",
    "    'city':['NY','NY','LA','NY','SF','LA']\n",
    "}\n",
    "df = pd.DataFrame(data)"
   ],
   "id": "ea741d7e3f89b427",
   "outputs": [],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:48:15.302966Z",
     "start_time": "2025-08-18T07:48:15.295942Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df.duplicated() #一整条记录都是一样的，标记为重复，返回True\n",
    "df.drop_duplicates(subset=['name']) #根据指定列去重\n",
    "df.drop_duplicates(subset=['name'],keep='last') #保留最后一次出现的行"
   ],
   "id": "d0c80234a279e93f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    name  age city\n",
       "3  alice   25   NY\n",
       "4   jack   35   SF\n",
       "5    bob   30   LA"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>city</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>alice</td>\n",
       "      <td>25</td>\n",
       "      <td>NY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>jack</td>\n",
       "      <td>35</td>\n",
       "      <td>SF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>bob</td>\n",
       "      <td>30</td>\n",
       "      <td>LA</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:48:40.641785Z",
     "start_time": "2025-08-18T07:48:40.634537Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数据类型的转换\n",
    "df = pd.read_csv('data/sleep.csv')\n",
    "df.dtypes"
   ],
   "id": "60fc85f957aa3580",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "person_id                    int64\n",
       "gender                      object\n",
       "age                          int64\n",
       "occupation                  object\n",
       "sleep_duration             float64\n",
       "sleep_quality              float64\n",
       "physical_activity_level      int64\n",
       "stress_level                 int64\n",
       "bmi_category                object\n",
       "blood_pressure              object\n",
       "heart_rate                   int64\n",
       "daily_steps                  int64\n",
       "sleep_disorder              object\n",
       "dtype: object"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:48:52.641334Z",
     "start_time": "2025-08-18T07:48:52.638111Z"
    }
   },
   "cell_type": "code",
   "source": "df['age'] = df['age'].astype('int16')",
   "id": "a28a3aaa318bfcbc",
   "outputs": [],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:49:02.487298Z",
     "start_time": "2025-08-18T07:49:02.483980Z"
    }
   },
   "cell_type": "code",
   "source": "df['gender'] = df['gender'].astype('category')",
   "id": "360b48874628f4e8",
   "outputs": [],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:49:11.850900Z",
     "start_time": "2025-08-18T07:49:11.845625Z"
    }
   },
   "cell_type": "code",
   "source": "df.gender",
   "id": "3f754eb9e8c52dd0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        Male\n",
       "1      Female\n",
       "2        Male\n",
       "3        Male\n",
       "4        Male\n",
       "        ...  \n",
       "395    Female\n",
       "396    Female\n",
       "397    Female\n",
       "398    Female\n",
       "399      Male\n",
       "Name: gender, Length: 400, dtype: category\n",
       "Categories (2, object): ['Female', 'Male']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:49:22.064119Z",
     "start_time": "2025-08-18T07:49:22.060512Z"
    }
   },
   "cell_type": "code",
   "source": "df['is_male'] = df['gender'].map({'Female':True,'Male':False})",
   "id": "d0d62e4e49504d88",
   "outputs": [],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:49:34.626270Z",
     "start_time": "2025-08-18T07:49:34.621095Z"
    }
   },
   "cell_type": "code",
   "source": "df.is_male",
   "id": "ec9bad0245b151a4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      False\n",
       "1       True\n",
       "2      False\n",
       "3      False\n",
       "4      False\n",
       "       ...  \n",
       "395     True\n",
       "396     True\n",
       "397     True\n",
       "398     True\n",
       "399    False\n",
       "Name: is_male, Length: 400, dtype: category\n",
       "Categories (2, bool): [True, False]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:49:49.534313Z",
     "start_time": "2025-08-18T07:49:49.518351Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数据变形\n",
    "import pandas as pd\n",
    "data = {\n",
    "    'ID': [1, 2],\n",
    "    'name':['alice','bob'],\n",
    "    'Math': [90, 85],\n",
    "    'English': [88, 92],\n",
    "    'Science': [95, 89]\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "print(df)\n",
    "df.T   #行列转置\n",
    "# 宽表转换成长表\n",
    "df2 = pd.melt(df,id_vars=['ID','name'],var_name='科目',value_name='分数')\n",
    "df2.sort_values('name')\n",
    "print(df2)\n",
    "# 长表转宽表\n",
    "pd.pivot(df2,index=['ID','name'],columns='科目',values='分数')"
   ],
   "id": "67cd609770a06230",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   ID   name  Math  English  Science\n",
      "0   1  alice    90       88       95\n",
      "1   2    bob    85       92       89\n",
      "   ID   name       科目  分数\n",
      "0   1  alice     Math  90\n",
      "1   2    bob     Math  85\n",
      "2   1  alice  English  88\n",
      "3   2    bob  English  92\n",
      "4   1  alice  Science  95\n",
      "5   2    bob  Science  89\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "科目        English  Math  Science\n",
       "ID name                         \n",
       "1  alice       88    90       95\n",
       "2  bob         92    85       89"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>科目</th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "      <th>Science</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ID</th>\n",
       "      <th>name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <th>alice</th>\n",
       "      <td>88</td>\n",
       "      <td>90</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <th>bob</th>\n",
       "      <td>92</td>\n",
       "      <td>85</td>\n",
       "      <td>89</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:50:11.932754Z",
     "start_time": "2025-08-18T07:50:11.918068Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = {\n",
    "    'ID': [1, 2],\n",
    "    'name':['alice smith','bob smith'],\n",
    "    'Math': [90, 85],\n",
    "    'English': [88, 92],\n",
    "    'Science': [95, 89]\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "# 分列\n",
    "df[['first','last']]  = df['name'].str.split(\" \",expand=True)\n",
    "df = pd.read_csv('data/sleep.csv')\n",
    "df = df[['person_id','blood_pressure']]\n",
    "df[['high','low']] = df['blood_pressure'].str.split('/',expand=True)\n",
    "df['high']=df['high'].astype('int64')\n",
    "df['low']=df['low'].astype('int64')\n",
    "df.info()\n",
    "df.high.mean()\n",
    "df.low.mean()"
   ],
   "id": "bc0c33fea60dfa3e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 400 entries, 0 to 399\n",
      "Data columns (total 4 columns):\n",
      " #   Column          Non-Null Count  Dtype \n",
      "---  ------          --------------  ----- \n",
      " 0   person_id       400 non-null    int64 \n",
      " 1   blood_pressure  400 non-null    object\n",
      " 2   high            400 non-null    int64 \n",
      " 3   low             400 non-null    int64 \n",
      "dtypes: int64(3), object(1)\n",
      "memory usage: 12.6+ KB\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "np.float64(73.04)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:50:25.307468Z",
     "start_time": "2025-08-18T07:50:25.288228Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数据分箱 pd.cut(x,bins,labels)\n",
    "import pandas as pd\n",
    "df = pd.read_csv('data/employees.csv')\n",
    "df.head(10)"
   ],
   "id": "b2d67d44d4204e84",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   employee_id first_name  last_name     email  phone_number      job_id  \\\n",
       "0          100     Steven       King     SKING  515.123.4567     AD_PRES   \n",
       "1          101      N_ann    Kochhar  NKOCHHAR  515.123.4568       AD_VP   \n",
       "2          102        Lex    De Haan   LDEHAAN  515.123.4569       AD_VP   \n",
       "3          103  Alexander     Hunold   AHUNOLD  590.423.4567     IT_PROG   \n",
       "4          104      Bruce      Ernst    BERNST  590.423.4568     IT_PROG   \n",
       "5          105      David     Austin   DAUSTIN  590.423.4569     IT_PROG   \n",
       "6          106      Valli  Pataballa  VPATABAL  590.423.4560     IT_PROG   \n",
       "7          107      Diana    Lorentz  DLORENTZ  590.423.5567     IT_PROG   \n",
       "8          108      Nancy  Greenberg  NGREENBE  515.124.4569      FI_MGR   \n",
       "9          109     Daniel     Faviet   DFAVIET  515.124.4169  FI_ACCOUNT   \n",
       "\n",
       "    salary  commission_pct  manager_id  department_id  \n",
       "0  24000.0             NaN         NaN           90.0  \n",
       "1  17000.0             NaN       100.0           90.0  \n",
       "2  17000.0             NaN       100.0           90.0  \n",
       "3   9000.0             NaN       102.0           60.0  \n",
       "4   6000.0             NaN       103.0           60.0  \n",
       "5   4800.0             NaN       103.0           60.0  \n",
       "6   4800.0             NaN       103.0           60.0  \n",
       "7   4200.0             NaN       103.0           60.0  \n",
       "8  12000.0             NaN       101.0          100.0  \n",
       "9   9000.0             NaN       108.0          100.0  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>employee_id</th>\n",
       "      <th>first_name</th>\n",
       "      <th>last_name</th>\n",
       "      <th>email</th>\n",
       "      <th>phone_number</th>\n",
       "      <th>job_id</th>\n",
       "      <th>salary</th>\n",
       "      <th>commission_pct</th>\n",
       "      <th>manager_id</th>\n",
       "      <th>department_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>100</td>\n",
       "      <td>Steven</td>\n",
       "      <td>King</td>\n",
       "      <td>SKING</td>\n",
       "      <td>515.123.4567</td>\n",
       "      <td>AD_PRES</td>\n",
       "      <td>24000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>90.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>101</td>\n",
       "      <td>N_ann</td>\n",
       "      <td>Kochhar</td>\n",
       "      <td>NKOCHHAR</td>\n",
       "      <td>515.123.4568</td>\n",
       "      <td>AD_VP</td>\n",
       "      <td>17000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>100.0</td>\n",
       "      <td>90.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>102</td>\n",
       "      <td>Lex</td>\n",
       "      <td>De Haan</td>\n",
       "      <td>LDEHAAN</td>\n",
       "      <td>515.123.4569</td>\n",
       "      <td>AD_VP</td>\n",
       "      <td>17000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>100.0</td>\n",
       "      <td>90.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>103</td>\n",
       "      <td>Alexander</td>\n",
       "      <td>Hunold</td>\n",
       "      <td>AHUNOLD</td>\n",
       "      <td>590.423.4567</td>\n",
       "      <td>IT_PROG</td>\n",
       "      <td>9000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>102.0</td>\n",
       "      <td>60.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>104</td>\n",
       "      <td>Bruce</td>\n",
       "      <td>Ernst</td>\n",
       "      <td>BERNST</td>\n",
       "      <td>590.423.4568</td>\n",
       "      <td>IT_PROG</td>\n",
       "      <td>6000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>103.0</td>\n",
       "      <td>60.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>105</td>\n",
       "      <td>David</td>\n",
       "      <td>Austin</td>\n",
       "      <td>DAUSTIN</td>\n",
       "      <td>590.423.4569</td>\n",
       "      <td>IT_PROG</td>\n",
       "      <td>4800.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>103.0</td>\n",
       "      <td>60.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>106</td>\n",
       "      <td>Valli</td>\n",
       "      <td>Pataballa</td>\n",
       "      <td>VPATABAL</td>\n",
       "      <td>590.423.4560</td>\n",
       "      <td>IT_PROG</td>\n",
       "      <td>4800.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>103.0</td>\n",
       "      <td>60.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>107</td>\n",
       "      <td>Diana</td>\n",
       "      <td>Lorentz</td>\n",
       "      <td>DLORENTZ</td>\n",
       "      <td>590.423.5567</td>\n",
       "      <td>IT_PROG</td>\n",
       "      <td>4200.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>103.0</td>\n",
       "      <td>60.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>108</td>\n",
       "      <td>Nancy</td>\n",
       "      <td>Greenberg</td>\n",
       "      <td>NGREENBE</td>\n",
       "      <td>515.124.4569</td>\n",
       "      <td>FI_MGR</td>\n",
       "      <td>12000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>101.0</td>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>109</td>\n",
       "      <td>Daniel</td>\n",
       "      <td>Faviet</td>\n",
       "      <td>DFAVIET</td>\n",
       "      <td>515.124.4169</td>\n",
       "      <td>FI_ACCOUNT</td>\n",
       "      <td>9000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>108.0</td>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:50:38.199732Z",
     "start_time": "2025-08-18T07:50:38.193288Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df1 = df.head(10)[['employee_id','salary']]\n",
    "df1"
   ],
   "id": "c2a455d287e2eb0f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   employee_id   salary\n",
       "0          100  24000.0\n",
       "1          101  17000.0\n",
       "2          102  17000.0\n",
       "3          103   9000.0\n",
       "4          104   6000.0\n",
       "5          105   4800.0\n",
       "6          106   4800.0\n",
       "7          107   4200.0\n",
       "8          108  12000.0\n",
       "9          109   9000.0"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>employee_id</th>\n",
       "      <th>salary</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>100</td>\n",
       "      <td>24000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>101</td>\n",
       "      <td>17000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>102</td>\n",
       "      <td>17000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>103</td>\n",
       "      <td>9000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>104</td>\n",
       "      <td>6000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>105</td>\n",
       "      <td>4800.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>106</td>\n",
       "      <td>4800.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>107</td>\n",
       "      <td>4200.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>108</td>\n",
       "      <td>12000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>109</td>\n",
       "      <td>9000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:50:56.132724Z",
     "start_time": "2025-08-18T07:50:56.118858Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pd.cut(df1['salary'],bins=3) #bins=n，分成n段区间，起始值、结束值是所有数据的最小值、最大值\n",
    "#4180~14100~24000\n",
    "pd.cut(df1['salary'],bins=3).value_counts()\n",
    "pd.cut(df1['salary'],bins=[0,10000,20000,30000])#bins=list，分成n段区间\n",
    "pd.cut(df1['salary'],bins=[0,10000,20000,30000]).value_counts()\n",
    "df1['收入范围'] =pd.cut(df1['salary'],bins=[0,10000,20000,30000],labels=['低','中','高'])#bins=list，分成n段区间\n",
    "pd.qcut(df1['salary'],3).value_counts()"
   ],
   "id": "f0552c45867d14cb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "salary\n",
       "(12000.0, 24000.0]    4\n",
       "(4199.999, 6000.0]    3\n",
       "(6000.0, 12000.0]     3\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:51:12.967069Z",
     "start_time": "2025-08-18T07:51:12.958604Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 睡眠数据\n",
    "df = pd.read_csv('data/sleep.csv')\n",
    "df1 = df.head(10)[['person_id','sleep_quality']]\n",
    "df1\n",
    "df['睡眠质量'] = pd.cut(df['sleep_quality'],bins=3,labels=\n",
    "                         ['差','中','优'])\n",
    "df['睡眠质量'].value_counts()\n",
    "df.head(10)\n",
    "df['gender']=df['gender'].astype('category')\n",
    "df['gender'].value_counts()\n",
    "# 字符串-->类别-->统计\n",
    "# 数值-->分箱-->统计\n",
    "print(df['gender'].dtype)\n",
    "print(df['睡眠质量'].dtype)"
   ],
   "id": "b2656e04c4bc2a8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "category\n",
      "category\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:51:26.841619Z",
     "start_time": "2025-08-18T07:51:26.833438Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.DataFrame({\n",
    "    'name':['jack','alice','tom','bob'],\n",
    "    'age':[20,30,40,50],\n",
    "    'gender':['female','male','female','male']\n",
    "})\n",
    "df.set_index(\"name\",inplace=True)\n",
    "df.reset_index(inplace=True)\n",
    "df.rename(columns={\"age\":\"年龄\"},index={0:4})"
   ],
   "id": "6e0ca7c4d38879b4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    name  年龄  gender\n",
       "4   jack  20  female\n",
       "1  alice  30    male\n",
       "2    tom  40  female\n",
       "3    bob  50    male"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>年龄</th>\n",
       "      <th>gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>jack</td>\n",
       "      <td>20</td>\n",
       "      <td>female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>alice</td>\n",
       "      <td>30</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>tom</td>\n",
       "      <td>40</td>\n",
       "      <td>female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>bob</td>\n",
       "      <td>50</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:51:42.179583Z",
     "start_time": "2025-08-18T07:51:42.172597Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df.index=[1,2,3,4]\n",
    "df.columns=[\"姓名\",'年龄',\"性别\"]\n",
    "df"
   ],
   "id": "b08ea20a5c68f4ba",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      姓名  年龄      性别\n",
       "1   jack  20  female\n",
       "2  alice  30    male\n",
       "3    tom  40  female\n",
       "4    bob  50    male"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>姓名</th>\n",
       "      <th>年龄</th>\n",
       "      <th>性别</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>jack</td>\n",
       "      <td>20</td>\n",
       "      <td>female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>alice</td>\n",
       "      <td>30</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>tom</td>\n",
       "      <td>40</td>\n",
       "      <td>female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>bob</td>\n",
       "      <td>50</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:52:03.652029Z",
     "start_time": "2025-08-18T07:52:03.639623Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 分组聚合\n",
    "# df.groupby('分组的字段')['聚合的字段'].聚合函数()\n",
    "import pandas as pd\n",
    "df = pd.read_csv('data/employees.csv')\n",
    "df = df.dropna(subset=['department_id'])\n",
    "df['department_id'] = df['department_id'].astype('int64')\n",
    "# 计算不同部门的平均薪资\n",
    "df.groupby('department_id').groups #查看分组\n",
    "df.groupby('department_id').get_group(20) #查看具体的某个分组数据\n",
    "df2 = df.groupby('department_id')[['salary']].mean()\n",
    "df2['salary'] = df2['salary'].round(2)\n",
    "df2=df2.reset_index()\n",
    "df2.sort_values('salary',ascending=False)"
   ],
   "id": "fe242851564fce3a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    department_id    salary\n",
       "8              90  19333.33\n",
       "10            110  10150.00\n",
       "6              70  10000.00\n",
       "1              20   9500.00\n",
       "7              80   8955.88\n",
       "9             100   8600.00\n",
       "3              40   6500.00\n",
       "5              60   5760.00\n",
       "0              10   4400.00\n",
       "2              30   4150.00\n",
       "4              50   3475.56"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>department_id</th>\n",
       "      <th>salary</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>90</td>\n",
       "      <td>19333.33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>110</td>\n",
       "      <td>10150.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>70</td>\n",
       "      <td>10000.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20</td>\n",
       "      <td>9500.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>80</td>\n",
       "      <td>8955.88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>100</td>\n",
       "      <td>8600.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>40</td>\n",
       "      <td>6500.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>60</td>\n",
       "      <td>5760.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>4400.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>30</td>\n",
       "      <td>4150.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>50</td>\n",
       "      <td>3475.56</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:52:15.683023Z",
     "start_time": "2025-08-18T07:52:15.673339Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算不同部门不同岗位的人的平均薪资\n",
    "df2=df.groupby(['department_id','job_id'])[['salary']].mean()\n",
    "df2=df2.reset_index()\n",
    "df2['salary'] = df2['salary'].round(1)\n",
    "df2.sort_values('salary',ascending=False)"
   ],
   "id": "d277f59904a68517",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    department_id      job_id   salary\n",
       "13             90     AD_PRES  24000.0\n",
       "14             90       AD_VP  17000.0\n",
       "1              20      MK_MAN  13000.0\n",
       "11             80      SA_MAN  12200.0\n",
       "16            100      FI_MGR  12000.0\n",
       "18            110      AC_MGR  12000.0\n",
       "4              30      PU_MAN  11000.0\n",
       "10             70      PR_REP  10000.0\n",
       "12             80      SA_REP   8396.6\n",
       "17            110  AC_ACCOUNT   8300.0\n",
       "15            100  FI_ACCOUNT   7920.0\n",
       "8              50      ST_MAN   7280.0\n",
       "5              40      HR_REP   6500.0\n",
       "2              20      MK_REP   6000.0\n",
       "9              60     IT_PROG   5760.0\n",
       "0              10     AD_ASST   4400.0\n",
       "6              50    SH_CLERK   3215.0\n",
       "7              50    ST_CLERK   2785.0\n",
       "3              30    PU_CLERK   2780.0"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>department_id</th>\n",
       "      <th>job_id</th>\n",
       "      <th>salary</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>90</td>\n",
       "      <td>AD_PRES</td>\n",
       "      <td>24000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>90</td>\n",
       "      <td>AD_VP</td>\n",
       "      <td>17000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20</td>\n",
       "      <td>MK_MAN</td>\n",
       "      <td>13000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>80</td>\n",
       "      <td>SA_MAN</td>\n",
       "      <td>12200.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>100</td>\n",
       "      <td>FI_MGR</td>\n",
       "      <td>12000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>110</td>\n",
       "      <td>AC_MGR</td>\n",
       "      <td>12000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>30</td>\n",
       "      <td>PU_MAN</td>\n",
       "      <td>11000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>70</td>\n",
       "      <td>PR_REP</td>\n",
       "      <td>10000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>80</td>\n",
       "      <td>SA_REP</td>\n",
       "      <td>8396.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>110</td>\n",
       "      <td>AC_ACCOUNT</td>\n",
       "      <td>8300.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>100</td>\n",
       "      <td>FI_ACCOUNT</td>\n",
       "      <td>7920.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>50</td>\n",
       "      <td>ST_MAN</td>\n",
       "      <td>7280.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>40</td>\n",
       "      <td>HR_REP</td>\n",
       "      <td>6500.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>20</td>\n",
       "      <td>MK_REP</td>\n",
       "      <td>6000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>60</td>\n",
       "      <td>IT_PROG</td>\n",
       "      <td>5760.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>AD_ASST</td>\n",
       "      <td>4400.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>50</td>\n",
       "      <td>SH_CLERK</td>\n",
       "      <td>3215.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>50</td>\n",
       "      <td>ST_CLERK</td>\n",
       "      <td>2785.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>30</td>\n",
       "      <td>PU_CLERK</td>\n",
       "      <td>2780.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:52:33.449312Z",
     "start_time": "2025-08-18T07:52:33.441261Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 企鹅数据分析\n",
    "# 1. 导入必要的库\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 2. 导入数据 喙\n",
    "df = pd.read_csv('data/penguins.csv')\n",
    "df.head(5)\n",
    "df.info()\n"
   ],
   "id": "3bcb4f3f86b6ed71",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 344 entries, 0 to 343\n",
      "Data columns (total 7 columns):\n",
      " #   Column             Non-Null Count  Dtype  \n",
      "---  ------             --------------  -----  \n",
      " 0   species            344 non-null    object \n",
      " 1   island             344 non-null    object \n",
      " 2   bill_length_mm     342 non-null    float64\n",
      " 3   bill_depth_mm      342 non-null    float64\n",
      " 4   flipper_length_mm  342 non-null    float64\n",
      " 5   body_mass_g        342 non-null    float64\n",
      " 6   sex                333 non-null    object \n",
      "dtypes: float64(4), object(3)\n",
      "memory usage: 18.9+ KB\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:52:45.028470Z",
     "start_time": "2025-08-18T07:52:45.024061Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 3. 数据清洗\n",
    "# 缺失值的检查\n",
    "print(df.isna().sum())\n",
    "df.dropna(inplace=True)"
   ],
   "id": "72096624a251e9ba",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "species               0\n",
      "island                0\n",
      "bill_length_mm        2\n",
      "bill_depth_mm         2\n",
      "flipper_length_mm     2\n",
      "body_mass_g           2\n",
      "sex                  11\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:52:54.700336Z",
     "start_time": "2025-08-18T07:52:54.690285Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 4. 数据特征的构造\n",
    "df['sex'] = df['sex'].astype('category')\n",
    "df['bill_ratio'] = df['bill_length_mm']/df['bill_depth_mm']\n",
    "df.head()"
   ],
   "id": "a50faa39d37081a6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  species     island  bill_length_mm  bill_depth_mm  flipper_length_mm  \\\n",
       "0  Adelie  Torgersen            39.1           18.7              181.0   \n",
       "1  Adelie  Torgersen            39.5           17.4              186.0   \n",
       "2  Adelie  Torgersen            40.3           18.0              195.0   \n",
       "4  Adelie  Torgersen            36.7           19.3              193.0   \n",
       "5  Adelie  Torgersen            39.3           20.6              190.0   \n",
       "\n",
       "   body_mass_g     sex  bill_ratio  \n",
       "0       3750.0    Male    2.090909  \n",
       "1       3800.0  Female    2.270115  \n",
       "2       3250.0  Female    2.238889  \n",
       "4       3450.0  Female    1.901554  \n",
       "5       3650.0    Male    1.907767  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>species</th>\n",
       "      <th>island</th>\n",
       "      <th>bill_length_mm</th>\n",
       "      <th>bill_depth_mm</th>\n",
       "      <th>flipper_length_mm</th>\n",
       "      <th>body_mass_g</th>\n",
       "      <th>sex</th>\n",
       "      <th>bill_ratio</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Adelie</td>\n",
       "      <td>Torgersen</td>\n",
       "      <td>39.1</td>\n",
       "      <td>18.7</td>\n",
       "      <td>181.0</td>\n",
       "      <td>3750.0</td>\n",
       "      <td>Male</td>\n",
       "      <td>2.090909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Adelie</td>\n",
       "      <td>Torgersen</td>\n",
       "      <td>39.5</td>\n",
       "      <td>17.4</td>\n",
       "      <td>186.0</td>\n",
       "      <td>3800.0</td>\n",
       "      <td>Female</td>\n",
       "      <td>2.270115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Adelie</td>\n",
       "      <td>Torgersen</td>\n",
       "      <td>40.3</td>\n",
       "      <td>18.0</td>\n",
       "      <td>195.0</td>\n",
       "      <td>3250.0</td>\n",
       "      <td>Female</td>\n",
       "      <td>2.238889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Adelie</td>\n",
       "      <td>Torgersen</td>\n",
       "      <td>36.7</td>\n",
       "      <td>19.3</td>\n",
       "      <td>193.0</td>\n",
       "      <td>3450.0</td>\n",
       "      <td>Female</td>\n",
       "      <td>1.901554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Adelie</td>\n",
       "      <td>Torgersen</td>\n",
       "      <td>39.3</td>\n",
       "      <td>20.6</td>\n",
       "      <td>190.0</td>\n",
       "      <td>3650.0</td>\n",
       "      <td>Male</td>\n",
       "      <td>1.907767</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:53:06.014680Z",
     "start_time": "2025-08-18T07:53:05.990833Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 5. 数据分析\n",
    "# 数据分箱-把体重分为三个等级\n",
    "labels = ['低','中','高']\n",
    "df['mass_level'] = pd.cut(df['body_mass_g'],bins=3,labels=labels)\n",
    "print(df['mass_level'].value_counts())\n",
    "# 按岛屿、性别分组分析\n",
    "df.groupby(['sex','island']).agg({\n",
    "    'body_mass_g':['mean','count'],\n",
    "})"
   ],
   "id": "1befd1f5aacd8381",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mass_level\n",
      "低    150\n",
      "中    128\n",
      "高     55\n",
      "Name: count, dtype: int64\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\49247\\AppData\\Local\\Temp\\ipykernel_19876\\4290054476.py:7: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  df.groupby(['sex','island']).agg({\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "                  body_mass_g      \n",
       "                         mean count\n",
       "sex    island                      \n",
       "Female Biscoe     4319.375000    80\n",
       "       Dream      3446.311475    61\n",
       "       Torgersen  3395.833333    24\n",
       "Male   Biscoe     5104.518072    83\n",
       "       Dream      3987.096774    62\n",
       "       Torgersen  4034.782609    23"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">body_mass_g</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sex</th>\n",
       "      <th>island</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">Female</th>\n",
       "      <th>Biscoe</th>\n",
       "      <td>4319.375000</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dream</th>\n",
       "      <td>3446.311475</td>\n",
       "      <td>61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Torgersen</th>\n",
       "      <td>3395.833333</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">Male</th>\n",
       "      <th>Biscoe</th>\n",
       "      <td>5104.518072</td>\n",
       "      <td>83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dream</th>\n",
       "      <td>3987.096774</td>\n",
       "      <td>62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Torgersen</th>\n",
       "      <td>4034.782609</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:53:28.269852Z",
     "start_time": "2025-08-18T07:53:28.239418Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 睡眠质量分析\n",
    "# 1.导入库\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 2.导入数据\n",
    "df = pd.read_csv('data/sleep.csv')\n",
    "df.head()\n",
    "df.info()\n",
    "df.describe()"
   ],
   "id": "3c77f4e08191c8a9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 400 entries, 0 to 399\n",
      "Data columns (total 13 columns):\n",
      " #   Column                   Non-Null Count  Dtype  \n",
      "---  ------                   --------------  -----  \n",
      " 0   person_id                400 non-null    int64  \n",
      " 1   gender                   400 non-null    object \n",
      " 2   age                      400 non-null    int64  \n",
      " 3   occupation               400 non-null    object \n",
      " 4   sleep_duration           400 non-null    float64\n",
      " 5   sleep_quality            400 non-null    float64\n",
      " 6   physical_activity_level  400 non-null    int64  \n",
      " 7   stress_level             400 non-null    int64  \n",
      " 8   bmi_category             400 non-null    object \n",
      " 9   blood_pressure           400 non-null    object \n",
      " 10  heart_rate               400 non-null    int64  \n",
      " 11  daily_steps              400 non-null    int64  \n",
      " 12  sleep_disorder           110 non-null    object \n",
      "dtypes: float64(2), int64(6), object(5)\n",
      "memory usage: 40.8+ KB\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "        person_id         age  sleep_duration  sleep_quality  \\\n",
       "count  400.000000  400.000000      400.000000     400.000000   \n",
       "mean   200.500000   39.950000        8.041250       6.125750   \n",
       "std    115.614301   14.038883        2.390787       1.975733   \n",
       "min      1.000000   18.000000        4.100000       1.000000   \n",
       "25%    100.750000   29.000000        5.900000       4.700000   \n",
       "50%    200.500000   40.000000        8.200000       6.100000   \n",
       "75%    300.250000   49.000000       10.125000       7.425000   \n",
       "max    400.000000   90.000000       12.000000      10.000000   \n",
       "\n",
       "       physical_activity_level  stress_level  heart_rate   daily_steps  \n",
       "count               400.000000     400.00000  400.000000    400.000000  \n",
       "mean                 64.985000       5.47250   75.990000  11076.510000  \n",
       "std                  32.297874       2.80873   15.099334   5364.789364  \n",
       "min                  10.000000       1.00000   50.000000   2067.000000  \n",
       "25%                  35.000000       3.00000   63.000000   6165.250000  \n",
       "50%                  65.500000       5.00000   77.000000  11785.500000  \n",
       "75%                  94.000000       8.00000   90.000000  15878.000000  \n",
       "max                 120.000000      10.00000  100.000000  19958.000000  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>person_id</th>\n",
       "      <th>age</th>\n",
       "      <th>sleep_duration</th>\n",
       "      <th>sleep_quality</th>\n",
       "      <th>physical_activity_level</th>\n",
       "      <th>stress_level</th>\n",
       "      <th>heart_rate</th>\n",
       "      <th>daily_steps</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>400.000000</td>\n",
       "      <td>400.000000</td>\n",
       "      <td>400.000000</td>\n",
       "      <td>400.000000</td>\n",
       "      <td>400.000000</td>\n",
       "      <td>400.00000</td>\n",
       "      <td>400.000000</td>\n",
       "      <td>400.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>200.500000</td>\n",
       "      <td>39.950000</td>\n",
       "      <td>8.041250</td>\n",
       "      <td>6.125750</td>\n",
       "      <td>64.985000</td>\n",
       "      <td>5.47250</td>\n",
       "      <td>75.990000</td>\n",
       "      <td>11076.510000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>115.614301</td>\n",
       "      <td>14.038883</td>\n",
       "      <td>2.390787</td>\n",
       "      <td>1.975733</td>\n",
       "      <td>32.297874</td>\n",
       "      <td>2.80873</td>\n",
       "      <td>15.099334</td>\n",
       "      <td>5364.789364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>18.000000</td>\n",
       "      <td>4.100000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>50.000000</td>\n",
       "      <td>2067.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>100.750000</td>\n",
       "      <td>29.000000</td>\n",
       "      <td>5.900000</td>\n",
       "      <td>4.700000</td>\n",
       "      <td>35.000000</td>\n",
       "      <td>3.00000</td>\n",
       "      <td>63.000000</td>\n",
       "      <td>6165.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>200.500000</td>\n",
       "      <td>40.000000</td>\n",
       "      <td>8.200000</td>\n",
       "      <td>6.100000</td>\n",
       "      <td>65.500000</td>\n",
       "      <td>5.00000</td>\n",
       "      <td>77.000000</td>\n",
       "      <td>11785.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>300.250000</td>\n",
       "      <td>49.000000</td>\n",
       "      <td>10.125000</td>\n",
       "      <td>7.425000</td>\n",
       "      <td>94.000000</td>\n",
       "      <td>8.00000</td>\n",
       "      <td>90.000000</td>\n",
       "      <td>15878.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>400.000000</td>\n",
       "      <td>90.000000</td>\n",
       "      <td>12.000000</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>120.000000</td>\n",
       "      <td>10.00000</td>\n",
       "      <td>100.000000</td>\n",
       "      <td>19958.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:53:43.109555Z",
     "start_time": "2025-08-18T07:53:43.105309Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 3.数据清洗\n",
    "df.isna().sum()\n",
    "df.drop(columns='sleep_disorder',inplace=True)"
   ],
   "id": "bd11abcc9a3bc45c",
   "outputs": [],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:53:54.608572Z",
     "start_time": "2025-08-18T07:53:54.585471Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 4. 数据特征的构造\n",
    "df['gender'] = df['gender'].astype('category')\n",
    "df['occupation'] = df['occupation'].astype('category')\n",
    "df['bmi_category'] = df['bmi_category'].astype('category')\n",
    "df[['high','low']]=df['blood_pressure'].str.split('/',expand=True)\n",
    "\n",
    "# 睡眠质量的分箱\n",
    "labels = ['差','中','优']\n",
    "df['quality_level'] = pd.cut(df['sleep_quality'],bins=3,labels=labels)\n",
    "age_labels=['青少年','中年','老年']\n",
    "df['age_level'] = pd.cut(df['age'],bins=3,labels=age_labels)\n",
    "df.head()"
   ],
   "id": "7836f2677a56abed",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   person_id  gender  age     occupation  sleep_duration  sleep_quality  \\\n",
       "0          1    Male   29   Manual Labor             7.4            7.0   \n",
       "1          2  Female   43        Retired             4.2            4.9   \n",
       "2          3    Male   44        Retired             6.1            6.0   \n",
       "3          4    Male   29  Office Worker             8.3           10.0   \n",
       "4          5    Male   67        Retired             9.1            9.5   \n",
       "\n",
       "   physical_activity_level  stress_level bmi_category blood_pressure  \\\n",
       "0                       41             7        Obese         124/70   \n",
       "1                       41             5        Obese         131/86   \n",
       "2                      107             4  Underweight         122/70   \n",
       "3                       20            10        Obese         124/72   \n",
       "4                       19             4   Overweight         133/78   \n",
       "\n",
       "   heart_rate  daily_steps high low quality_level age_level  \n",
       "0          91         8539  124  70             中       青少年  \n",
       "1          81        18754  131  86             中        中年  \n",
       "2          81         2857  122  70             中        中年  \n",
       "3          55         6886  124  72             优       青少年  \n",
       "4          97        14945  133  78             优        老年  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>person_id</th>\n",
       "      <th>gender</th>\n",
       "      <th>age</th>\n",
       "      <th>occupation</th>\n",
       "      <th>sleep_duration</th>\n",
       "      <th>sleep_quality</th>\n",
       "      <th>physical_activity_level</th>\n",
       "      <th>stress_level</th>\n",
       "      <th>bmi_category</th>\n",
       "      <th>blood_pressure</th>\n",
       "      <th>heart_rate</th>\n",
       "      <th>daily_steps</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>quality_level</th>\n",
       "      <th>age_level</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Male</td>\n",
       "      <td>29</td>\n",
       "      <td>Manual Labor</td>\n",
       "      <td>7.4</td>\n",
       "      <td>7.0</td>\n",
       "      <td>41</td>\n",
       "      <td>7</td>\n",
       "      <td>Obese</td>\n",
       "      <td>124/70</td>\n",
       "      <td>91</td>\n",
       "      <td>8539</td>\n",
       "      <td>124</td>\n",
       "      <td>70</td>\n",
       "      <td>中</td>\n",
       "      <td>青少年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Female</td>\n",
       "      <td>43</td>\n",
       "      <td>Retired</td>\n",
       "      <td>4.2</td>\n",
       "      <td>4.9</td>\n",
       "      <td>41</td>\n",
       "      <td>5</td>\n",
       "      <td>Obese</td>\n",
       "      <td>131/86</td>\n",
       "      <td>81</td>\n",
       "      <td>18754</td>\n",
       "      <td>131</td>\n",
       "      <td>86</td>\n",
       "      <td>中</td>\n",
       "      <td>中年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Male</td>\n",
       "      <td>44</td>\n",
       "      <td>Retired</td>\n",
       "      <td>6.1</td>\n",
       "      <td>6.0</td>\n",
       "      <td>107</td>\n",
       "      <td>4</td>\n",
       "      <td>Underweight</td>\n",
       "      <td>122/70</td>\n",
       "      <td>81</td>\n",
       "      <td>2857</td>\n",
       "      <td>122</td>\n",
       "      <td>70</td>\n",
       "      <td>中</td>\n",
       "      <td>中年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>Male</td>\n",
       "      <td>29</td>\n",
       "      <td>Office Worker</td>\n",
       "      <td>8.3</td>\n",
       "      <td>10.0</td>\n",
       "      <td>20</td>\n",
       "      <td>10</td>\n",
       "      <td>Obese</td>\n",
       "      <td>124/72</td>\n",
       "      <td>55</td>\n",
       "      <td>6886</td>\n",
       "      <td>124</td>\n",
       "      <td>72</td>\n",
       "      <td>优</td>\n",
       "      <td>青少年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>Male</td>\n",
       "      <td>67</td>\n",
       "      <td>Retired</td>\n",
       "      <td>9.1</td>\n",
       "      <td>9.5</td>\n",
       "      <td>19</td>\n",
       "      <td>4</td>\n",
       "      <td>Overweight</td>\n",
       "      <td>133/78</td>\n",
       "      <td>97</td>\n",
       "      <td>14945</td>\n",
       "      <td>133</td>\n",
       "      <td>78</td>\n",
       "      <td>优</td>\n",
       "      <td>老年</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:54:06.474826Z",
     "start_time": "2025-08-18T07:54:06.470705Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 5.数据的统计、分析\n",
    "print(df['bmi_category'].value_counts())"
   ],
   "id": "59fb0710c36b212c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bmi_category\n",
      "Overweight     109\n",
      "Underweight    102\n",
      "Obese           98\n",
      "Normal          91\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:54:18.062586Z",
     "start_time": "2025-08-18T07:54:18.050901Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 根据不同的bmi分组，睡眠质量\n",
    "df.groupby(['age_level','bmi_category']).agg({\n",
    "    'sleep_duration':'mean',\n",
    "    'sleep_quality':'mean',\n",
    "    'stress_level':'mean'\n",
    "})"
   ],
   "id": "5d2ef1d2802a886c",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\49247\\AppData\\Local\\Temp\\ipykernel_19876\\1339789783.py:2: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  df.groupby(['age_level','bmi_category']).agg({\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "                        sleep_duration  sleep_quality  stress_level\n",
       "age_level bmi_category                                             \n",
       "青少年       Normal              8.100000       6.332000      4.860000\n",
       "          Obese               8.250000       6.253448      5.534483\n",
       "          Overweight          8.214286       6.171429      5.317460\n",
       "          Underweight         7.603279       5.883607      5.426230\n",
       "中年        Normal              7.422222       6.650000      4.944444\n",
       "          Obese               7.805556       6.216667      5.888889\n",
       "          Overweight          8.246154       5.956410      5.974359\n",
       "          Underweight         8.497500       5.907500      5.750000\n",
       "老年        Normal              7.420000       4.240000      4.200000\n",
       "          Obese               7.900000       5.025000      8.000000\n",
       "          Overweight          8.971429       6.285714      6.714286\n",
       "          Underweight        10.500000       6.200000      6.000000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>sleep_duration</th>\n",
       "      <th>sleep_quality</th>\n",
       "      <th>stress_level</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>age_level</th>\n",
       "      <th>bmi_category</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">青少年</th>\n",
       "      <th>Normal</th>\n",
       "      <td>8.100000</td>\n",
       "      <td>6.332000</td>\n",
       "      <td>4.860000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Obese</th>\n",
       "      <td>8.250000</td>\n",
       "      <td>6.253448</td>\n",
       "      <td>5.534483</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Overweight</th>\n",
       "      <td>8.214286</td>\n",
       "      <td>6.171429</td>\n",
       "      <td>5.317460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Underweight</th>\n",
       "      <td>7.603279</td>\n",
       "      <td>5.883607</td>\n",
       "      <td>5.426230</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">中年</th>\n",
       "      <th>Normal</th>\n",
       "      <td>7.422222</td>\n",
       "      <td>6.650000</td>\n",
       "      <td>4.944444</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Obese</th>\n",
       "      <td>7.805556</td>\n",
       "      <td>6.216667</td>\n",
       "      <td>5.888889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Overweight</th>\n",
       "      <td>8.246154</td>\n",
       "      <td>5.956410</td>\n",
       "      <td>5.974359</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Underweight</th>\n",
       "      <td>8.497500</td>\n",
       "      <td>5.907500</td>\n",
       "      <td>5.750000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">老年</th>\n",
       "      <th>Normal</th>\n",
       "      <td>7.420000</td>\n",
       "      <td>4.240000</td>\n",
       "      <td>4.200000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Obese</th>\n",
       "      <td>7.900000</td>\n",
       "      <td>5.025000</td>\n",
       "      <td>8.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Overweight</th>\n",
       "      <td>8.971429</td>\n",
       "      <td>6.285714</td>\n",
       "      <td>6.714286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Underweight</th>\n",
       "      <td>10.500000</td>\n",
       "      <td>6.200000</td>\n",
       "      <td>6.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 39
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
